<?php
if($_POST['xmlhttp']){
	include_once('config.php');

	$linkid = $_POST['linkid'];
	$ownerid = $_POST['ownerid'];
	//check if already insert
	if ($db->get_var('SELECT COUNT(lo_id) FROM ' . table_link_owner . ' WHERE link_id=' . $linkid . ' AND owner_id=' . $ownerid) > 0) {
		echo 'success';
		die();
	}
	$signal = $db->query('INSERT INTO ' . table_link_owner . ' (link_id, owner_id, percent) VALUES(' . $linkid . ', ' . $ownerid . ', 100)');
	if ($signal > 0) {
		include_once(mnminclude.'KMessaging.php');
		include_once(mnminclude.'link.php');
		$thinglink = new Link();
		$thinglink->id = $linkid;
		$thinglink->read();
		$message =  new KMessaging();
		$message->tblName = table_messages;
		$title = '您已被设置为 ' . $thinglink->title . ' 的所有者';
		$body = '您已被设置为 <a href="story.php?id=' . $linkid . '" target="_blank">' . $thinglink->title . '</a> 的所有者';
		$sender = 1;
		$receiver = $ownerid;
		$senderlevel = 0;
		$sendmsg = $message->SendMessege($title,$body,$sender,$receiver,$senderlevel);//even if the mail fail to deliver, we think we succeed in set owner
		echo 'success';
	}
	else {
		echo 'fail';
	}
}
?>